package com.easy.system.service.impl;

import com.easy.system.service.BizService;
import com.easy.system.util.Constant;
import com.easy.system.util.Pagination;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: sunguangzhu
 * Date: 13-8-4
 * Time: 下午12:30
 * To change this template use File | Settings | File Templates.
 */

public class BizServiceImpl<T> extends BaseServiceImpl<T> implements BizService<T> {
    public BizServiceImpl() {

    }

    public BizServiceImpl(Class<T> clazz) {
        super.setClazz(clazz);
    }

    @Override
    public List<T> getAdvanceSearch(Pagination pagination, Object[] properties, Object[] params) {
        StringBuilder hql = new StringBuilder(" from " + super.clazz.getSimpleName());
        hql = this.getAdvanceSearchHql(hql, properties);
        return this.baseDao.find(hql.toString(), pagination, params);
        //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public List<T> getSimpleSearch(Pagination pagination, Object[] properties, Object[] params) {
        StringBuilder hql = new StringBuilder(" from " + super.clazz.getSimpleName());
        hql = this.getSimpleSearchHql(hql, properties);
        return this.baseDao.find(hql.toString(), pagination, params);
        //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public Long getAdvanceSearchTotal(Object[] properties, Object[] params) {
        StringBuilder hql = new StringBuilder(" select count(id) from ").append(clazz.getSimpleName());
        hql = this.getAdvanceSearchHql(hql, properties);
        return this.baseDao.getTotal(hql.toString(), params);
        //To change body of created methods use File | Settings | File Templates.
    }

    @Override
    public Long getSimpleSearchTotal(Object[] properties, Object[] params) {
        StringBuilder hql = new StringBuilder(" select count(id) from ").append(clazz.getSimpleName());
        hql = this.getSimpleSearchHql(hql, properties);
        return this.baseDao.getTotal(hql.toString(), params);
        //To change body of implemented methods use File | Settings | File Templates.
    }

    private StringBuilder getAdvanceSearchHql(StringBuilder hql, Object[] properties) {
        if (properties != null) {
            hql.append(" where ");
            for (int i = 0; i < properties.length; i++) {
                hql.append(Constant.BLANK + properties[i]).append(" = ").append(" ? ");
                if (i < properties.length - 1) {
                    hql.append(" and ");
                }
            }
        }
        hql.append(" order by id");
        return hql;
    }

    private StringBuilder getSimpleSearchHql(StringBuilder hql, Object[] properties) {
        if (properties != null) {
            hql.append(" where ");
            for (int i = 0; i < properties.length; i++) {
                hql.append(Constant.BLANK + properties[i]).append(" like ").append(" ? ");
                if (i < properties.length - 1) {
                    hql.append(" or ");
                }
            }
        }
        hql.append(" order by id");
        return hql;
    }

}
